﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gd.IBiz;
using gd.Utils;
using gd.ViewModel.ExcelImport;
using gd.ViewModel.ExcelImport.UserModule;

namespace gd.Biz.ExcelImport.Impl.Demo
{
    /// <summary>
    /// 菜单导入（演示）
    /// </summary>
    public class MenuImport : IExcelImport
    {
        public object GetViewData(object obj)
        {
            var usrId = obj as string;

            // 视图数据
            var data = new gd.ViewModel.ExcelImport.UserModule.CustomerViewData();
            data.CustomerType = new Dictionary<int, string>();
            data.CustomerType.Add(0, "普通客户");
            data.CustomerType.Add(1, "批发客户");
            data.CustomerType.Add(2, "黑名单客户");

            data.Users = new Dictionary<int, string>();
            data.Users.Add(1, "小赵");
            data.Users.Add(2, "小李");

            return data;
        }

        public gd.ViewModel.ExcelImport.Result ImportCheck(string id, object submitData, IList<object> importData)
        {
            var list = new List<ResultRow>();
            var rv = new gd.ViewModel.ExcelImport.Result()
            {
                Rows = list
            };

            //var form = 将表单提交上来的formData数据转换成具体的form对象 gd.ViewModel.ExcelImport.UserModule.CustomerSubmitData
            var data = ConvertX.ModelToModel<IList<CustomerExcelRow>>(importData);

            var i = 0;
            foreach (var item in data)
            {
                i++;
                var r = new ResultRow()
                {
                    Index = i,
                    Success = (i % 2) == 0,
                    FirstField = item.Name
                };
                if (r.Success == false)
                {
                    r.ResultMessage = "用户已存在";
                }
                else
                {
                    r.ResultMessage = "导入成功";
                }
                list.Add(r);
            }
            
            rv.Success = (DateTime.Now.Second % 2 == 0);
            rv.Message = rv.Success ? "" : "这不是演习";
            return rv;
        }

        public gd.ViewModel.ExcelImport.Result ImportStart(string id, object formData, IList<object> importData)
        {
            throw new NotImplementedException();
        }

    }
}
